package com.example.crossborderecommerce.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.crossborderecommerce.entity.CustomerOrder;
import com.example.crossborderecommerce.entity.LogisticsProvider;
import com.example.crossborderecommerce.entity.LogisticsProviderOrder;
import com.example.crossborderecommerce.entity.Result;
import com.example.crossborderecommerce.mapper.LogisticsProviderOrderMapper;
import com.example.crossborderecommerce.service.ICustomerOrderService;
import com.example.crossborderecommerce.service.ILogisticsProviderOrderService;
import com.example.crossborderecommerce.service.ILogisticsProviderService;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 物流商订单模块
 *
 * @author Yolanda
 * @since 2024-05-09
 */
@RestController
@Transactional
@RequestMapping("/logistics-provider-order")
public class LogisticsProviderOrderController {
    @Resource
    ILogisticsProviderOrderService logisticsProviderOrderService;

    @Resource
    ICustomerOrderService customerOrderService;

    @Resource
    ILogisticsProviderService logisticsProviderService;

    @Resource
    LogisticsProviderOrderMapper logisticsProviderOrderMapper;

    /**
     * 添加物流商订单
     *
     * @param logisticsProviderOrder
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody LogisticsProviderOrder logisticsProviderOrder) {
        LocalDateTime now = LocalDateTime.now();
        logisticsProviderOrder.setStartTime(now);
        boolean res = logisticsProviderOrderService.save(logisticsProviderOrder);
        if (res) {
            return Result.success("添加物流商订单成功！");
        }
        return Result.error("添加物流商订单失败！");
    }

    /**
     * 删除物流订单
     * @param logisticsProviderOrder
     * @return
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody LogisticsProviderOrder logisticsProviderOrder){
        boolean res = logisticsProviderOrderService.removeById(logisticsProviderOrder);
        if(res){
            return Result.success("删除物流订单订单成功！");
        }
        return Result.error("删除物流订单订单失败！");
    }
    
    /**
     * 删除一系列物流商订单
     * @param ids
     * @return
     */
    @GetMapping("/deleteBatch")
    public Result deleteBatch(@RequestParam String ids) {
        List<String> logisticsProviderOrderIds = Arrays.stream(ids.split(",")).collect(Collectors.toList());
        boolean res = logisticsProviderOrderService.removeByIds(logisticsProviderOrderIds);
        if(res) {
            return Result.success("删除物流商订单成功！");
        }
        return Result.error("删除物流商订单失败！");
    }

    /**
     * 修改物流商订单
     * @param logisticsProviderOrder
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody LogisticsProviderOrder logisticsProviderOrder) {
        boolean res = logisticsProviderOrderService.saveOrUpdate(logisticsProviderOrder);
        if (res) {
            return Result.success("修改物流商订单成功！");
        }
        return Result.error("修改物流商订单失败！");
    }

    /**
     * 根据客户订单找到物流订单，修改状态为已完成（在设置客户订单为已完成时）
     * @param customerOrderId
     * @return
     */
    @GetMapping ("/updateByCustomerId")
    public Result updateByCustomerId(@RequestParam String customerOrderId) {
        LogisticsProviderOrder logisticsProviderOrder = logisticsProviderOrderMapper.getByCustomerOrderId(customerOrderId);
        logisticsProviderOrder.setStatus("已送达");
        boolean res = logisticsProviderOrderService.saveOrUpdate(logisticsProviderOrder);
        if (res) {
            return Result.success("修改物流商订单成功！");
        }
        return Result.error("修改物流商订单失败！");
    }

    /**
     * 分页查询物流订单
     * @param id
     * @param name
     * @param status
     * @param customerOrderId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/findPage")
    public Result findPage(@RequestParam(defaultValue = "") String id,
                           @RequestParam(defaultValue = "") String name,
                           @RequestParam(defaultValue = "") String status,
                           @RequestParam(defaultValue = "") String customerOrderId,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<LogisticsProviderOrder> queryWrapper = new QueryWrapper<>();
        if(!status.equals("全部") && !status.isEmpty()) {
            queryWrapper.eq("status", status);
        }
        queryWrapper.like("id", id);
        queryWrapper.like("name", name);
        queryWrapper.like("customer_order_id", customerOrderId);
        Page<LogisticsProviderOrder> logisticsProviderOrderPage = logisticsProviderOrderService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success("成功查询所有物流商信息！",logisticsProviderOrderPage);
    }
    /**
     * 导出物流订单
     * @param id
     * @param name
     * @param status
     * @param customerOrderId
     * @param response
     * @return
     */
    @GetMapping("exportLogisticsProviderOrderList")
    public Result exportLogisticsProviderOrderList(@RequestParam(defaultValue = "") String id,
                                         @RequestParam(defaultValue = "") String name,
                                         @RequestParam(defaultValue = "") String status,
                                         @RequestParam(defaultValue = "") String customerOrderId,
                                         HttpServletResponse response) {
        QueryWrapper<LogisticsProviderOrder> queryWrapper = new QueryWrapper<>();
        if(!status.equals("全部") && !status.isEmpty()) {
            queryWrapper.eq("status", status);
        }
        queryWrapper.like("id", id);
        queryWrapper.like("name", name);
        queryWrapper.like("customer_order_id", customerOrderId);
        List<LogisticsProviderOrder> logisticsProviderOrderList = logisticsProviderOrderService.list(queryWrapper);
        HSSFWorkbook wb = new HSSFWorkbook(); // 导出到excel文件中
        HSSFSheet sheet = wb.createSheet("表一");
        HSSFRow row = sheet.createRow(0);
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        HSSFCell cell = row.createCell((short) 0);
        cell.setCellValue("ID");
        cell.setCellStyle(style);
        cell = row.createCell((short) 1);
        cell.setCellValue("客户订单编号");
        cell.setCellStyle(style);
        cell = row.createCell((short) 2);
        cell.setCellValue("配送方式");
        cell.setCellStyle(style);
        cell = row.createCell((short) 3);
        cell.setCellValue("物流费用");
        cell.setCellStyle(style);
        cell = row.createCell((short)4);
        cell.setCellValue("运输状态");
        cell.setCellStyle(style);
        cell = row.createCell((short)5);
        cell.setCellValue("开始时间");
        cell.setCellStyle(style);
        cell = row.createCell((short)6);
        cell.setCellValue("结束时间");
        cell.setCellStyle(style);
        cell = row.createCell((short)7);
        cell.setCellValue("物流商");
        cell.setCellStyle(style);
        cell = row.createCell((short)8);
        cell.setCellValue("联系电话");
        cell.setCellStyle(style);
        SimpleDateFormat inputDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        SimpleDateFormat outputDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (int i = 0; i < logisticsProviderOrderList.size(); i++) {
            row = sheet.createRow(i + 1);
            LogisticsProviderOrder logisticsProviderOrder = logisticsProviderOrderList.get(i);
            row.createCell((short) 0).setCellValue(logisticsProviderOrder.getId());
            row.createCell((short) 1).setCellValue(logisticsProviderOrder.getCustomerOrderId());
            row.createCell((short) 2).setCellValue(logisticsProviderOrder.getLogisticsChannels());
            row.createCell((short) 3).setCellValue(logisticsProviderOrder.getPrice());
            row.createCell((short) 4).setCellValue(logisticsProviderOrder.getStatus());
            row.createCell((short) 7).setCellValue(logisticsProviderOrder.getName());
            row.createCell((short) 8).setCellValue(logisticsProviderOrder.getPhone());
            try {
                Date startTime=inputDateFormat.parse(logisticsProviderOrder.getStartTime().toString());
                Date endTime = inputDateFormat.parse(logisticsProviderOrder.getEndTime().toString()); // 假设 getCreateTime 返回的是 String
                row.createCell((short) 5).setCellValue(outputDateFormat.format(startTime));
                row.createCell((short) 6).setCellValue(outputDateFormat.format(endTime));

            } catch (ParseException e) {
                row.createCell((short) 5).setCellValue(logisticsProviderOrder.getStartTime().toString());
                row.createCell((short) 6).setCellValue(logisticsProviderOrder.getEndTime().toString());
            }
        }
        String fileName = "exportLogisticsProviderOrderList.xls";
        // 用户下载
        try {
            ServletOutputStream outputStream = null;
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(fileName, "UTF-8"));
            outputStream = response.getOutputStream();
            wb.write(outputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return Result.success("exportLogisticsProviderOrderList");
    }
    /**
     * 根据物流状态获取货物数量
     * @return
     */
    @GetMapping("/findCommoditiesByStatus")
    public Result findCommoditiesStatus(@RequestParam(defaultValue = "") String status){
        QueryWrapper<LogisticsProviderOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",status);
        List<LogisticsProviderOrder> logisticsProviderOrderList = logisticsProviderOrderService.list(queryWrapper);//先查有哪些仓库
        if(logisticsProviderOrderList==null){
            return Result.success(0);
        }
        int sum=0;
        for (LogisticsProviderOrder logisticsProviderOrder:logisticsProviderOrderList){
           CustomerOrder customerOrder =customerOrderService.getById(logisticsProviderOrder.getCustomerOrderId());
           sum=sum+customerOrder.getQuantity();

        }
        return Result.success(sum);
    }

    /**
     * 物流成本
     * @return
     */
    @GetMapping("/getLogisticProviderCost")
    public Result getLogisticProviderCost() {
        return Result.success(logisticsProviderOrderMapper.getLogisticProviderCost());
    }

    /**
     * 获取近七天物流费
     * @return
     */
    @GetMapping("/getLogisticsProviderCostInWeek")
    public Result getLogisticsProviderCostInWeek() {
        LocalDate today = LocalDate.now();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Double> logisticsProviderCostInWeek = new LinkedList<>();
        // 循环七天
        for (int i = 6; i >= 0; --i) {
            // 今天的日期
            LocalDate currentDay = today.plusDays(-i);
            // 构建今天的开始时间（0时0分0秒）
            LocalDateTime startOfDay = LocalDateTime.of(currentDay, LocalTime.MIDNIGHT);
            // 构建今天的结束时间（接近午夜的一秒前，这里简化为23:59:59）
            LocalDateTime endOfDay = LocalDateTime.of(currentDay, LocalTime.of(23, 59, 59));
            // 将LocalDateTime转换为Date
            Date startDate = Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
            Date endDate = Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
            // 格式化日期并打印
            String formattedStartDate = sdf.format(startDate);
            String formattedEndDate = sdf.format(endDate);
            double cost = logisticsProviderOrderMapper.getLogisticProviderCostDuration(formattedStartDate, formattedEndDate);
            logisticsProviderCostInWeek.add(cost);
        }
        return Result.success(logisticsProviderCostInWeek);
    }

    /**
     * 获取昨天的物流费
     * @return
     */
    @GetMapping("/getLogisticsProviderCostInYesterday")
    public Result getLogisticsProviderCostInYesterday() {
        LocalDate today = LocalDate.now();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        LocalDate currentDay = today.plusDays(-1);
        LocalDateTime startOfDay = LocalDateTime.of(currentDay, LocalTime.MIDNIGHT);
        LocalDateTime endOfDay = LocalDateTime.of(currentDay, LocalTime.of(23, 59, 59));
        Date startDate = Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
        Date endDate = Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
        String formattedStartDate = sdf.format(startDate);
        String formattedEndDate = sdf.format(endDate);
        double cost = logisticsProviderOrderMapper.getLogisticProviderCostDuration(formattedStartDate, formattedEndDate);
        return Result.success(cost);
    }

    /**
     * 获取近七个月，每个月今天的物流费
     * @return
     */
    @GetMapping("/getLogisticsProviderCostInMonthToday")
    public Result getLogisticsProviderCostInMonthToday() {
        LocalDate today = LocalDate.now();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Double> logisticsProviderCostInMonthToday = new LinkedList<>();
        // 循环七天
        for (int i = 6; i >= 0; --i) {
            // 今天的日期
            LocalDate currentDay = today.plusMonths(-i);
            // 构建今天的开始时间（0时0分0秒）
            LocalDateTime startOfDay = LocalDateTime.of(currentDay, LocalTime.MIDNIGHT);
            // 构建今天的结束时间（接近午夜的一秒前，这里简化为23:59:59）
            LocalDateTime endOfDay = LocalDateTime.of(currentDay, LocalTime.of(23, 59, 59));
            // 将LocalDateTime转换为Date
            Date startDate = Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
            Date endDate = Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
            // 格式化日期并打印
            String formattedStartDate = sdf.format(startDate);
            String formattedEndDate = sdf.format(endDate);
            double cost = logisticsProviderOrderMapper.getLogisticProviderCostDuration(formattedStartDate, formattedEndDate);
            logisticsProviderCostInMonthToday.add(cost);
        }
        return Result.success(logisticsProviderCostInMonthToday);
    }

    /**
     * 获取近七个月，每个月总的物流费
     * @return
     */
    @GetMapping("/getLogisticsProviderCostInMonth")
    public Result getLogisticsProviderCostInMonth() {
        LocalDate today = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        List<Double> logisticsProviderCostInMonth = new LinkedList<>();
        for (int i = 6; i >=0; --i) {
            LocalDate firstDayOfMonth = today.minusMonths(i).with(TemporalAdjusters.firstDayOfMonth());
            LocalDate lastDayOfMonth = today.minusMonths(i).with(TemporalAdjusters.lastDayOfMonth());
            LocalDateTime startOfDay = firstDayOfMonth.atStartOfDay();
            LocalDateTime endOfDay = lastDayOfMonth.atTime(23, 59, 59);
            String formattedStartDate = formatter.format(startOfDay);
            String formattedEndDate = formatter.format(endOfDay);
            double cost = logisticsProviderOrderMapper.getLogisticProviderCostDuration(formattedStartDate, formattedEndDate);
            logisticsProviderCostInMonth.add(cost);
        }
        return Result.success(logisticsProviderCostInMonth);
    }

    /**
     * 获取近一年，每个月总的物流费
     * @return
     */
    @GetMapping("/getLogisticsProviderCostInYear")
    public Result getLogisticsProviderCostInYear() {
        LocalDate today = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        List<Double> logisticsProviderCostInYear = new LinkedList<>();
        for (int i = 11; i >=0; --i) {
            LocalDate firstDayOfMonth = today.minusMonths(i).with(TemporalAdjusters.firstDayOfMonth());
            LocalDate lastDayOfMonth = today.minusMonths(i).with(TemporalAdjusters.lastDayOfMonth());
            LocalDateTime startOfDay = firstDayOfMonth.atStartOfDay();
            LocalDateTime endOfDay = lastDayOfMonth.atTime(23, 59, 59);
            String formattedStartDate = formatter.format(startOfDay);
            String formattedEndDate = formatter.format(endOfDay);
            double cost = logisticsProviderOrderMapper.getLogisticProviderCostDuration(formattedStartDate, formattedEndDate);
            logisticsProviderCostInYear.add(cost);
        }
        return Result.success(logisticsProviderCostInYear);
    }

    /**
     * 获取近今天的物流费
     * @return
     */
    @GetMapping("/getLogisticsProviderCostInToday")
    public Result getLogisticsProviderCostInToday() {
        LocalDate today = LocalDate.now();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        LocalDate currentDay = today.plusDays(0);
        LocalDateTime startOfDay = LocalDateTime.of(currentDay, LocalTime.MIDNIGHT);
        LocalDateTime endOfDay = LocalDateTime.of(currentDay, LocalTime.of(23, 59, 59));
        Date startDate = Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
        Date endDate = Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
        String formattedStartDate = sdf.format(startDate);
        String formattedEndDate = sdf.format(endDate);
        double cost = logisticsProviderOrderMapper.getLogisticProviderCostDuration(formattedStartDate, formattedEndDate);
        return Result.success(cost);
    }

    /**
     * 获取今天的货物总量
     * @return
     */
    @GetMapping("/getLogisticsProviderQuantityInToday")
    public Result getLogisticsProviderQuantityInToday() {
        LocalDate today = LocalDate.now();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        LocalDate currentDay = today.plusDays(0);
        LocalDateTime startOfDay = LocalDateTime.of(currentDay, LocalTime.MIDNIGHT);
        LocalDateTime endOfDay = LocalDateTime.of(currentDay, LocalTime.of(23, 59, 59));
        Date startDate = Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
        Date endDate = Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
        String formattedStartDate = sdf.format(startDate);
        String formattedEndDate = sdf.format(endDate);
        Integer quantity = logisticsProviderOrderMapper.getLogisticProviderQuantityDuration(formattedStartDate, formattedEndDate);
        return Result.success(quantity);
    }

    /**
     * 获取昨天的货物总量
     * @return
     */
    @GetMapping("/getLogisticsProviderQuantityInYesterday")
    public Result getLogisticsProviderQuantityInYesterday() {
        LocalDate today = LocalDate.now();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        LocalDate currentDay = today.plusDays(-1);
        LocalDateTime startOfDay = LocalDateTime.of(currentDay, LocalTime.MIDNIGHT);
        LocalDateTime endOfDay = LocalDateTime.of(currentDay, LocalTime.of(23, 59, 59));
        Date startDate = Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
        Date endDate = Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
        String formattedStartDate = sdf.format(startDate);
        String formattedEndDate = sdf.format(endDate);
        Integer quantity = logisticsProviderOrderMapper.getLogisticProviderQuantityDuration(formattedStartDate, formattedEndDate);
        return Result.success(quantity);
    }

    /**
     * 获取近七个月，每个月今天的物流订单数
     * @return
     */
    @GetMapping("/getLogisticsProviderOrderQuantityInMonthToday")
    public Result getLogisticsProvideOrderQuantityInMonthToday() {
        LocalDate today = LocalDate.now();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Integer> logisticsProviderQuantityInMonthToday = new LinkedList<>();
        // 循环七天
        for (int i = 6; i >= 0; --i) {
            // 今天的日期
            LocalDate currentDay = today.plusMonths(-i);
            // 构建今天的开始时间（0时0分0秒）
            LocalDateTime startOfDay = LocalDateTime.of(currentDay, LocalTime.MIDNIGHT);
            // 构建今天的结束时间（接近午夜的一秒前，这里简化为23:59:59）
            LocalDateTime endOfDay = LocalDateTime.of(currentDay, LocalTime.of(23, 59, 59));
            // 将LocalDateTime转换为Date
            Date startDate = Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
            Date endDate = Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
            // 格式化日期并打印
            String formattedStartDate = sdf.format(startDate);
            String formattedEndDate = sdf.format(endDate);
            Integer cost = logisticsProviderOrderMapper.getLogisticProviderOrderQuantityDuration(formattedStartDate, formattedEndDate);
            logisticsProviderQuantityInMonthToday.add(cost);
        }
        return Result.success(logisticsProviderQuantityInMonthToday);
    }

    /**
     * 获取近七个月，每个月总的物流订单数
     * @return
     */
    @GetMapping("/getLogisticsProviderOrderQuantityInMonth")
    public Result getLogisticsProviderOrderQuantityInMonth() {
        LocalDate today = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        List<Integer> logisticsProviderQuantityInMonth = new LinkedList<>();
        for (int i = 6; i >=0; --i) {
            LocalDate firstDayOfMonth = today.minusMonths(i).with(TemporalAdjusters.firstDayOfMonth());
            LocalDate lastDayOfMonth = today.minusMonths(i).with(TemporalAdjusters.lastDayOfMonth());
            LocalDateTime startOfDay = firstDayOfMonth.atStartOfDay();
            LocalDateTime endOfDay = lastDayOfMonth.atTime(23, 59, 59);
            String formattedStartDate = formatter.format(startOfDay);
            String formattedEndDate = formatter.format(endOfDay);
            Integer cost = logisticsProviderOrderMapper.getLogisticProviderOrderQuantityDuration(formattedStartDate, formattedEndDate);
            logisticsProviderQuantityInMonth.add(cost);
        }
        return Result.success(logisticsProviderQuantityInMonth);
    }

    /**
     * 获取近一年，每个月总的物流订单数
     * @return
     */
    @GetMapping("/getLogisticsProviderOrderQuantityInYear")
    public Result getLogisticsProviderOrderQuantityInYear() {
        LocalDate today = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        List<Integer> logisticsProviderQuantityInYear = new LinkedList<>();
        for (int i = 11; i >=0; --i) {
            LocalDate firstDayOfMonth = today.minusMonths(i).with(TemporalAdjusters.firstDayOfMonth());
            LocalDate lastDayOfMonth = today.minusMonths(i).with(TemporalAdjusters.lastDayOfMonth());
            LocalDateTime startOfDay = firstDayOfMonth.atStartOfDay();
            LocalDateTime endOfDay = lastDayOfMonth.atTime(23, 59, 59);
            String formattedStartDate = formatter.format(startOfDay);
            String formattedEndDate = formatter.format(endOfDay);
            Integer cost = logisticsProviderOrderMapper.getLogisticProviderOrderQuantityDuration(formattedStartDate, formattedEndDate);
            logisticsProviderQuantityInYear.add(cost);
        }
        return Result.success(logisticsProviderQuantityInYear);
    }

    /**
     * 获取昨天的已送达订单数
     * @return
     */
    @GetMapping("/getLogisticsProviderOrderQuantityInYesterday")
    public Result getLogisticsProviderOrderQuantityInYesterday() {
        LocalDate today = LocalDate.now();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        LocalDate currentDay = today.plusDays(-1);
        LocalDateTime startOfDay = LocalDateTime.of(currentDay, LocalTime.MIDNIGHT);
        LocalDateTime endOfDay = LocalDateTime.of(currentDay, LocalTime.of(23, 59, 59));
        Date startDate = Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
        Date endDate = Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
        String formattedStartDate = sdf.format(startDate);
        String formattedEndDate = sdf.format(endDate);
        Integer quantity = logisticsProviderOrderMapper.getLogisticProviderOrderQuantityDuration(formattedStartDate, formattedEndDate);
        return Result.success(quantity);
    }

    /**
     * 根据物流状态获取物流订单数量
     * @param status
     * @return
     */
    @GetMapping("/getLogisticProviderOrderQuantityByStatus")
    public Result getLogisticProviderOrderQuantityByStatus(@RequestParam("status") String status) {
        return Result.success(logisticsProviderOrderMapper.getLogisticProviderOrderQuantityByStatus(status));
    }

    /**
     * 根据物流渠道获取物流订单数量
     * @param logisticsChannels
     * @return
     */
    @GetMapping("/getLogisticProviderOrderQuantityByChannel")
    public Result getLogisticProviderOrderQuantityByChannel(@RequestParam("logisticsChannels") String logisticsChannels) {
        return Result.success(logisticsProviderOrderMapper.getLogisticProviderOrderQuantityByChannel(logisticsChannels));
    }

    /**
     * 根据物流渠道获取物流订单总费用
     * @param logisticsChannels
     * @return
     */
    @GetMapping("/getLogisticProviderOrderPriceByChannel")
    public Result getLogisticProviderOrderPriceByChannel(@RequestParam("logisticsChannels") String logisticsChannels) {
        return Result.success(logisticsProviderOrderMapper.getLogisticProviderOrderPriceByChannel(logisticsChannels));
    }

    /**
     * 根据物流渠道获取物流订单运输货物总量
     * @param logisticsChannels
     * @return
     */
    @GetMapping("/getLogisticProviderOrderCommodityQuantityByChannel")
    public Result getLogisticProviderOrderCommodityQuantityByChannel(@RequestParam("logisticsChannels") String logisticsChannels) {
        return Result.success(logisticsProviderOrderMapper.getLogisticProviderOrderCommodityQuantityByChannel(logisticsChannels));
    }

    /**
     * 根据物流商名称获取物流订单运输货物总量
     * @param name
     * @return
     */
    @GetMapping("/getLogisticProviderOrderCommodityQuantityByName")
    public Result getLogisticProviderOrderCommodityQuantityByName(@RequestParam("name") String name) {
        return Result.success(logisticsProviderOrderMapper.getLogisticProviderOrderCommodityQuantityByName(name));
    }

    /**
     * 根据物流商名称获取物流订单数量
     * @param name
     * @return
     */
    @GetMapping("/getLogisticProviderOrderQuantityByName")
    public Result getLogisticProviderOrderQuantityByName(@RequestParam("name") String name) {
        return Result.success(logisticsProviderOrderMapper.getLogisticProviderOrderQuantityByName(name));
    }

    /**
     * 获取物流订单运输货物总量
     * @return
     */
    @GetMapping("/getLogisticProviderOrderCommodityQuantity")
    public Result getLogisticProviderOrderCommodityQuantity() {
        return Result.success(logisticsProviderOrderMapper.getLogisticProviderOrderCommodityQuantity());
    }

    /**
     * 获取已送达物流订单总量
     * @return
     */
    @GetMapping("/getLogisticProviderOrderQuantity")
    public Result getLogisticProviderOrderQuantity() {
        Integer quantity = logisticsProviderOrderMapper.getLogisticProviderOrderQuantityByStatus("已送达");
        return Result.success(quantity);
    }
}
